package com.dqhl.community.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.model.PropertyRental;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by guanlucoang on 2016/8/31.
 * 物业缴费金额 activity
 */
public class PropertyRentalActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_topTitle; //顶部标题
    private ImageView iv_topBack; //顶部返回键
    private Button btn_payment; //立即缴纳
    private Button btn_subscribe; //预交缴费

    private TextView tv_community_one; //所在小区
    private TextView tv_name; //缴费单编号
    private TextView tv_propertyZhinajin; //滞纳金、止数
    private TextView tv_price;//单价
    private TextView tv_unit;//单位
    private TextView tv_total_one; //应缴费用
    private TextView tv_total_two; //应缴费用（下）
    private TextView tv_start;
    private TextView tv_end;
    private TextView tv_end_time;
    private TextView tv_top_time;
    private TextView tv_propertyType;
    private TextView tv_Area;
    private TextView tv_address;  //地址：门牌号

    private String type; //缴费类型
    private String strMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_property_rental);
        // 实例化对象
        initView();
        // 实例化数据
        initData();
        //获取数据
        getData();
    }

    // 实例化数据
    private void initData() {
        tv_topTitle.setText(R.string.propertyPay);
        iv_topBack.setVisibility(View.VISIBLE);
        iv_topBack.setOnClickListener(this);
        btn_payment.setOnClickListener(this);
        btn_subscribe.setOnClickListener(this);
        type = getIntent().getStringExtra("type");

    }

    // 实例化对象
    private void initView() {
        tv_topTitle = (TextView) findViewById(R.id.tv_top_title);
        iv_topBack = (ImageView) findViewById(R.id.iv_top_back);
        btn_payment = (Button) findViewById(R.id.btn_payment);
        btn_subscribe = (Button) findViewById(R.id.btn_subscribe);
        tv_community_one = (TextView) findViewById(R.id.tv_community_one);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_name = (TextView) findViewById(R.id.tv_propertyName);
        tv_propertyType = (TextView) findViewById(R.id.tv_propertyType);
        tv_price = (TextView) findViewById(R.id.tv_propertyPrice);
        tv_unit = (TextView) findViewById(R.id.tv_pripertyUnit);
        tv_total_one = (TextView) findViewById(R.id.tv_total_one);
        tv_total_two = (TextView) findViewById(R.id.tv_propertyTotal);
        tv_start = (TextView) findViewById(R.id.tv_propertyArea);
        tv_end = (TextView) findViewById(R.id.tv_propertyEnd);
        tv_end_time = (TextView) findViewById(R.id.tv_propertyEndTime);
        tv_top_time = (TextView) findViewById(R.id.tv_propertyTopTime);
        tv_propertyZhinajin = (TextView) findViewById(R.id.tv_propertyZhinajin);
        tv_Area = (TextView) findViewById(R.id.tv_Area);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            //预交缴费
            case R.id.btn_subscribe:
                Bundle bundle1 = new Bundle();
                bundle1.putString("rentalType", type);
                bundle1.putString("total", strMoney);
                overlay(PropertyPredictActivity.class, bundle1);
                break;
            //立即缴纳
            case R.id.btn_payment:
                if(strMoney == null){
                    toast("暂无金额");
                }else{
                    Bundle bundle = new Bundle();
                    bundle.putString("rentalType", type);
                    bundle.putString("total", strMoney);
                    overlay(PropertyConfimActivity.class, bundle);
                }
                break;
        }
    }

    //获取数据
    private void getData() {
        showCustomLoadBar("加载中…");
        Dlog.e("GG缴费类型" + type);
        RequestParams params = new RequestParams(Config.Api.propertyPay);
        params.addBodyParameter("userid", app.getUserId());
        params.addBodyParameter("leixing", type);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e("GG缴费返回的数据" + result);
                int errCode = JsonUtils.getErrCode(result);
                String errMsg = JsonUtils.getInfo(result, "errMsg");
                String data = JsonUtils.getInfo(result, "data");
                if (errCode == 0) {

                    PropertyRental propertyRental = JSON.parseObject(data, PropertyRental.class);
                    tv_community_one.setText(propertyRental.getCommunity_id());
                    String strAddress = propertyRental.getCommunity_id() + propertyRental.getBuilding_id()
                            + propertyRental.getUnit_id() + propertyRental.getResidence_id();
                    tv_address.setText(strAddress);
                    tv_name.setText(propertyRental.getName());
                    tv_price.setText(propertyRental.getPrice());
                    tv_unit.setText(propertyRental.getUnit());
                    strMoney = propertyRental.getTotal();
                    tv_total_one.setText(strMoney);
                    tv_total_two.setText(propertyRental.getTotal());
                    tv_end.setText(propertyRental.getEnd());
                    tv_end_time.setText(propertyRental.getEnd_time());
                    tv_top_time.setText(propertyRental.getTop_time());
                    if ("1".equals(type)) {
                        tv_propertyType.setText("物业费");
                        tv_propertyZhinajin.setText("滞纳金");
                        tv_start.setText(propertyRental.getNum());
                    } else if ("2".equals(type)) {
                        tv_propertyType.setText("水费");
                        tv_Area.setText("起数");
                    } else if ("3".equals(type)) {
                        tv_propertyType.setText("电费");
                        tv_Area.setText("起数");
                    } else if ("4".equals(type)) {
                        tv_propertyType.setText("卫生费");
                        tv_start.setText(propertyRental.getNum());
                    } else if ("5".equals(type)) {
                        tv_propertyType.setText("燃气费");
                        tv_Area.setText("起数");
                    } else if ("6".equals(type)) {
                        tv_propertyType.setText("电梯费");
                        tv_propertyZhinajin.setText("滞纳金");
                        tv_start.setText(propertyRental.getNum());
                        tv_Area.setText("住户");
                    }

                } else if (errCode == 4013) {
                    toast(errMsg);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                toast(R.string.error_net_connection);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                hideCustomLoadBar();
            }
        });
    }
}
